package com.mediacloud.app.newsmodule.view

import android.graphics.Color
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.model.news.LiveProgramListReciver
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.util.TimeUtils
import org.jetbrains.anko.backgroundColor
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
Date:2021/7/21
Author:Wy
 */
class LeftAdapter(layoutResId: Int) : BaseQuickAdapter<LiveProgramListReciver.ProgramWeekItem, BaseViewHolder>(layoutResId) {
    override fun convert(helper: BaseViewHolder, item: LiveProgramListReciver.ProgramWeekItem?) {
        val text_day = helper.getView<TextView>(R.id.text_day)
        val view_now = helper.getView<View>(R.id.view_now)

        item?.let {
            if (TimeUtils.isToday(it.date, TimeUtils.YYYYMMDD_2_FORMAT)) {
                text_day.text = "今天"
            }else{
                var timeText = stampToTime(timeToStamp(it.date).toString())
                if (timeText.startsWith("0")) {
                    timeText = timeText.substring(1, timeText.length)
                }
                text_day.text = timeText
            }
            if (it.selected) {
                view_now.visibility = View.VISIBLE
                text_day.backgroundColor = Color.parseColor("#FFFFFF")
            } else {
                view_now.visibility = View.GONE
                text_day.backgroundColor = Color.parseColor("#F7F7F7")
            }
        }
    }

    /**
     * 时间戳转时间
     * */
    private fun stampToTime(stamp: String): String {
        val sdf = SimpleDateFormat("MM.dd");
        return sdf.format(Date(stamp.toLong())); // 时间戳转换日期;\
    }

    /**
     * 时间转时间戳
     * */
    private fun timeToStamp(timers: String): Long {
        var d = Date()
        try {
            val sf = SimpleDateFormat("yyyyMMdd")
            d = sf.parse(timers);// 日期转换为时间戳
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return d.time;
    }
}